<template>
  <div class="app-container">
    <page-tools
      search-txt="用户昵称"
      add-txt="新增用户"
      @onAdd="handleClick(0)"
    />
    <el-table :data="list" border style="width: 100%">
      <el-table-column
        fixed
        type="index"
        label="序列"
        width="50"
        align="center"
      />
      <el-table-column prop="avatar" label="头像" width="150" align="center">
        <template v-slot="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.avatar"
            fit="contain"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="nickname"
        label="昵称"
        width="150"
        align="center"
      />
      <el-table-column prop="email" label="邮箱号" align="center">
        <template v-slot="scope">
          <p v-if="scope.row.email">
            {{ scope.row.email }}
          </p>
          <p v-else>
            <el-tag type="danger">未填写</el-tag>
          </p>
        </template>
      </el-table-column>
      <el-table-column prop="role" label="用户角色" width="150" align="center">
        <template v-slot="scope">
          <p>
            <el-tag type="success">{{ scope.row.role_name }}</el-tag>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="用户状态"
        width="150"
        align="center"
      >
        <template v-slot="scope">
          <p>
            <el-switch
              v-model="scope.row.status"
              disabled
              :active-value="0"
              :inactive-value="1"
            />
          </p>
        </template>
      </el-table-column>
      <el-table-column
        prop="login_ip"
        label="登录IP"
        width="150"
        align="center"
      />
      <el-table-column
        prop="createTime"
        label="注册时间"
        width="200"
        align="center"
      >
        <template slot-scope="sope">
          {{ sope.row.createTime | getTime }}
        </template>
      </el-table-column>
      <el-table-column
        prop="lastTime"
        label="最后登录时间"
        width="200"
        align="center"
      >
        <template slot-scope="sope">
          <p v-if="sope.row.lastTime">
            {{ sope.row.lastTime | getTime }}
          </p>
          <p v-else>
            <el-tag type="danger">未登录</el-tag>
          </p>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleClick(scope.row)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <PagePagination
      :page="pageParams.page"
      :page-size="pageParams.pageSize"
      :total="total"
      @update-page="onUpdatePage"
      @update-size="onUpdateSize"
    />
    <el-dialog
      :title="`${title}用户`"
      :visible.sync="dialogVisible"
      width="40%"
      center
      @close="onClose"
    >
      <el-form ref="userRef" :model="userForm" :rules="userRules">
        <el-form-item label="用户头像" label-width="90px" prop="avatar">
          <image-upload ref="myStaffPhoto" />
        </el-form-item>
        <el-form-item label="用户名" label-width="90px" prop="username">
          <el-input v-model="userForm.username" autocomplete="off" />
        </el-form-item>
        <el-form-item
          v-if="title === '新增'"
          label="用户密码"
          label-width="90px"
          prop="password"
        >
          <el-input v-model="userForm.password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="用户昵称" label-width="90px" prop="nickname">
          <el-input v-model="userForm.nickname" autocomplete="off" />
        </el-form-item>
        <el-form-item label="邮箱" label-width="90px" prop="email">
          <el-input v-model="userForm.email" autocomplete="off" />
        </el-form-item>
        <el-form-item label="角色" label-width="90px" prop="role_id">
          <el-radio-group v-model="userForm.role_id" class="ml-4">
            <el-radio
              v-for="role in roleList"
              :key="role.id"
              :label="role.id"
              size="large"
              border
            >{{ role.description }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" label-width="90px" prop="status">
          <el-switch
            v-model="userForm.status"
            class="ml-2"
            :inactive-value="1"
            :active-value="0"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleListAPI } from '@/api/role'
import { getUserInfoAPI, getUserListAPI, postUserAPI, putUserInfoAPI } from '@/api/user'
import { userRules } from '@/utils/rules'

export default {
  data() {
    return {
      list: [],
      dialogVisible: false,
      userForm: {},
      pageParams: {
        page: 1,
        pageSize: 5
      },
      total: 0,
      roleList: [],
      title: '',
      userRules
    }
  },
  mounted() {
    this.getUserList()
    this.getRoleList()
  },
  methods: {
    async handleClick(row) {
      this.dialogVisible = true
      if (row.id) {
        const resU = await getUserInfoAPI(row.id)
        this.userForm = resU.data
        this.title = '编辑'
        this.$refs['myStaffPhoto'].fileList = [{ url: row.avatar, upload: true }]
      } else {
        this.title = '新增'
      }
    },
    onClose() {
      this.dialogVisible = false
      this.userForm = {}
      this.$refs['userRef'].clearValidate()
      this.$refs['myStaffPhoto'].fileList = []
    },
    async getUserList() {
      const res = await getUserListAPI(this.pageParams)
      this.list = res.data.result
      this.total = res.data.total
    },
    async getRoleList() {
      const resR = await getRoleListAPI()
      this.roleList = resR.data.result
    },
    onUpdatePage(page) {
      this.pageParams.page = page
      this.getUserList()
    },
    onUpdateSize(size) {
      this.pageParams.pageSize = size
      this.getUserList()
    },
    onSubmit() {
      const fileList = this.$refs['myStaffPhoto'].fileList
      this.userForm.avatar = fileList[0].url
      this.$refs['userRef'].validate(async valid => {
        if (valid) {
          if (this.userForm.id) {
            await putUserInfoAPI(this.userForm.id, this.userForm)
            this.getUserList()
            this.$message({
              message: '编辑用户信息成功',
              type: 'success'
            })
          } else {
            await postUserAPI(this.userForm)
            this.getUserList()
            this.$message({
              message: '新增用户信息成功',
              type: 'success'
            })
          }
          this.dialogVisible = false
        } else {
          this.$message.error('请填写完整信息')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
